package com.coder520.service.impl;

import com.coder520.common.Const;
import com.coder520.common.ServerResponse;
import com.coder520.dao.AttendMapper;
import com.coder520.dao.ReAttendMapper;
import com.coder520.pojo.Attend;
import com.coder520.pojo.ReAttend;
import com.coder520.service.IReAttendService;
import com.google.common.collect.Lists;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ASUS on 2017/7/8.
 */
@Service("iReAttendService")
public class ReAttendServiceImpl implements IReAttendService {

    private static final String RE_ATTEND_FLOW_ID = "re_attend";

    /**
     * 补签流程状态
     */
    private static final Byte RE_ATTEND_STATUS_ONGOING =1 ;
    private static final Byte RE_ATTEND_STATUS_PSSS =2 ;
    private static final Byte RE_ATTEND_STATUS_REFUSE =3 ;

    private static final Byte ATTEND_STATUS_NORMAL =1 ;
    /**
     * 流程下一步处理人
     */
    private static final String NEXT_HANDLER = "next_handler";
    /**
     * 任务关联补签数据键
     */
    private static final String RE_ATTEND_SIGN = "re_attend";


    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private ReAttendMapper reAttendMapper;

    @Autowired
    private AttendMapper attendMapper;

    @Transactional
    public void startAttendFlow(ReAttend reAttend){
        //查找到相关人员领导
        reAttend.setCurrentHandler("laowang666");
        reAttend.setStatus(RE_ATTEND_STATUS_ONGOING);
        //插入数据库补签表
        reAttendMapper.insertSelective(reAttend);

        Map<String,Object> map = new HashMap<>();

        map.put(RE_ATTEND_SIGN,reAttend);
        map.put(NEXT_HANDLER,reAttend.getCurrentHandler());
        //启动补签流程实例

        ProcessInstance processInstance =  runtimeService.startProcessInstanceByKey(RE_ATTEND_FLOW_ID,map);

        //提交用户补签任务
        Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();

        taskService.complete(task.getId(),map);
    }


    public ServerResponse<List<ReAttend>> listTask(String username){
        List<ReAttend> reAttendList = Lists.newArrayList();

        List<Task> taskList= taskService.createTaskQuery().processVariableValueEquals(username).list();

        //转成页面实体
        if(CollectionUtils.isNotEmpty(taskList)){
            for(Task task : taskList){
                Map<String,Object> map = taskService.getVariables(task.getId());

                ReAttend reAttend = (ReAttend) map.get(RE_ATTEND_SIGN);
                reAttend.setTaskId(task.getId());
                reAttendList.add(reAttend);
            }

        }
        return ServerResponse.createBySuccess(reAttendList);
    }

    public ServerResponse approve(ReAttend reAttend){
        boolean flag = false;
        if((""+RE_ATTEND_STATUS_PSSS).equals(reAttend.getApproveFlag())){
            //审批通过
            Attend attend = new Attend();
            attend.setId(reAttend.getAttendId());
            attend.setAttendStatus(Const.NORMAL);
            attendMapper.updateByPrimaryKeySelective(attend);
            reAttend.setStatus(RE_ATTEND_STATUS_PSSS);
            reAttendMapper.updateByPrimaryKeySelective(reAttend);
            flag = true;
        }else if((""+RE_ATTEND_STATUS_REFUSE).equals(reAttend.getApproveFlag())){
            reAttend.setStatus(RE_ATTEND_STATUS_REFUSE);
            reAttendMapper.updateByPrimaryKeySelective(reAttend);
            flag = false;
        }
        taskService.complete(reAttend.getTaskId());
        if(flag){
            return ServerResponse.createBySuccess("审批通过");
        }
        return ServerResponse.createByError("审批不通过");
    }

    public List<ReAttend> listReAttend(String username){

        return reAttendMapper.selectByUsername(username);

    }


}
